package com.qf.interceptor;

import com.qf.utils.APICounts;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;

//写完拦截器后需要配置拦截器拦截哪些请求 的 uri路径 (不包括http ip端口叫uri)
@Component //让spring启动时创建拦截器对象
public class ApiContInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("================");

        //1.获取请求的uri路径
        String requestURI = request.getRequestURI();
        //2.修改map中对应的uri的计数
        HashMap<String, Integer> apicountMap = APICounts.apicountMap;
        //获取uri的次数
        Integer integer = apicountMap.get(requestURI);
        //第一次就是1 反之加1
        int counts = integer == null ? 1 : integer + 1;
        apicountMap.put(requestURI,counts);
        return true;//请求可以继续调用后面的controller中的方法，返回false，那么请求到此为止
    }
}
